{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "8e2c7b6b",
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "from dotenv import load_dotenv\n",
    "from langchain_openai import ChatOpenAI\n",
    "\n",
    "load_dotenv()\n",
    "\n",
    "# Configure an LLM\n",
    "llm = ChatOpenAI(\n",
    "    model=\"qwen3-32b\",\n",
    "    temperature=0.5,\n",
    "    base_url=\"https://dashscope.aliyuncs.com/compatible-mode/v1\",\n",
    "    api_key=os.environ.get(\"AI_DASHSCOPE_API_KEY\"),\n",
    "    streaming=True,\n",
    "    extra_body={\"enable_thinking\": False}\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "724e1802",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "AIMessage(content='你好小明！很高兴认识你~我是Qwen，可以和你聊聊天、帮你解决问题。你今天过得怎么样呀？有什么我可以帮到你的吗？ 😊', additional_kwargs={}, response_metadata={'finish_reason': 'stop', 'model_name': 'qwen3-32b'}, id='run-ad7a0ba5-ff2e-42b9-8c13-98ca9c39e4b4-0')"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from langchain_core.messages import HumanMessage\n",
    "\n",
    "llm.invoke([HumanMessage(content=\"你好，我叫小明！\")])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "036f038a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "AIMessage(content='你叫小明！😊 如果你有其他问题或者需要帮助，随时告诉我哦！', additional_kwargs={}, response_metadata={'finish_reason': 'stop', 'model_name': 'qwen3-32b'}, id='run-cf86bde9-1d83-4138-889a-9f9689d6d78f-0')"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from langchain_core.messages import AIMessage\n",
    "\n",
    "llm.invoke(\n",
    "    [\n",
    "        HumanMessage(content=\"你好，我叫小明！\"),\n",
    "        AIMessage(content=\"你好小明！很高兴认识你。有什么我可以帮助你的吗？\"),\n",
    "        HumanMessage(content=\"我叫什么？\"),\n",
    "    ]\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "be53361d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "AIMessage(content='你好！我无法直接知道你的名字，因为这需要你主动告诉我。你能分享一下你的名字吗？这样我们可以更好地交流！ 😊', additional_kwargs={}, response_metadata={'finish_reason': 'stop', 'model_name': 'qwen3-32b'}, id='run-1950ea3f-031a-4d89-96ef-d8cef537c0ba-0')"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from langchain_core.messages import AIMessage\n",
    "\n",
    "llm.invoke(\n",
    "    [\n",
    "        HumanMessage(content=\"我叫什么？\")\n",
    "    ]\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "44c10398",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "|你好|小|明|！|很高兴认识你~|我是Qwen，|可以和你聊|聊天、帮你解决问题|。你今天过得|怎么样呀？有什么|我可以帮你的吗|？ 😊||"
     ]
    }
   ],
   "source": [
    "for chunk in llm.stream([HumanMessage(content=\"你好，我叫小明！\")]):\n",
    "    print(chunk.content, end=\"|\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "66932fd9",
   "metadata": {},
   "outputs": [],
   "source": [
    "from langchain_core.prompts import ChatPromptTemplate\n",
    "\n",
    "system_template = \"将下面的英文翻译成{language}\"\n",
    "\n",
    "prompt_template = ChatPromptTemplate.from_messages(\n",
    "    [(\"system\", system_template), (\"user\", \"{text}\")]\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "fbabc330",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "你好！\n"
     ]
    }
   ],
   "source": [
    "prompt = prompt_template.invoke({\"language\": \"中文\", \"text\": \"hi!\"})\n",
    "\n",
    "response = llm.invoke(prompt)\n",
    "\n",
    "print(response.content)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "b22761e1",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "こんにちは！\n"
     ]
    }
   ],
   "source": [
    "prompt = prompt_template.invoke({\"language\": \"日文\", \"text\": \"hi!\"})\n",
    "\n",
    "response = llm.invoke(prompt)\n",
    "print(response.content)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "5d27bcd9",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "你好！\n"
     ]
    }
   ],
   "source": [
    "translate_chain = prompt_template | llm\n",
    "\n",
    "response = translate_chain.invoke({\"language\": \"中文\", \"text\": \"hi!\"})\n",
    "print(response.content)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
